<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/30
 * Time: 16:14
 */

namespace App\LogicService;

use App\LogicService\Contracts\BloodGlucoseContrastLogicContract;
use App\Models\BloodGlucoseContrast;


class BloodGlucoseContrastLogic extends BaseLogic implements BloodGlucoseContrastLogicContract
{
    public function update($account, $value)
    {
        $bloodGlucoseContrast = BloodGlucoseContrast::firstOrNew(['account' => $account, 'type' => $value['type']]);
        $bloodGlucoseContrast->max_value = $value['max_value'];
        $bloodGlucoseContrast->min_value = $value['min_value'];
        $bloodGlucoseContrast->save();

        return $bloodGlucoseContrast;
    }

    public function getList($account)
    {
        $bloodGlucoseContrasts = BloodGlucoseContrast::where('account', $account)
            ->get()
            ->toArray();


        if(!$bloodGlucoseContrasts){
            $contrast_data = array(
                array('account'=>$account,'type'=>1,'min_value'=> 3.9,'max_value'=> 6.1),
                array('account'=>$account,'type'=>2,'min_value'=> 4.4,'max_value'=> 7.8),
                array('account'=>$account,'type'=>3,'min_value'=> 3.9,'max_value'=> 6.1),
                array('account'=>$account,'type'=>4,'min_value'=> 3.9,'max_value'=> 6.1)
            );
            foreach ($contrast_data as $key => $value) {
                $contrast = $this->update($account, $value);
                $bloodGlucoseContrasts[] = $contrast;
            }
        }
        return $bloodGlucoseContrasts;
    }
}